package drag2share;

import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class InfoServlet extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    OutputStreamWriter out = new OutputStreamWriter(resp.getOutputStream(), "UTF8");
    resp.setHeader("content-type", "text/plain; charset=utf-8");
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Query q = new Query("File").setKeysOnly();
    PreparedQuery pq = ds.prepare(q);
    out.write("[");
    String str = null;
    for (Entity entity : pq.asIterable())
    {
      if(str == null)
        str = "\""+entity.getKey().getName()+"\"";
      else
        str = ",\""+entity.getKey().getName()+"\"";
      out.write(str);
    }
    out.write("]\n");
    out.flush();
  }
}
